<?php

namespace app\index\controller;

use think\Controller;
use think\Request;

class Detail extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($id)
    {
        if (!intval($id)) {
            $this->error('ID不合法');
        }
        $deal = model('deal')->find($id);
        if (!$deal || $deal->status != 1) {
            $this->error('该商品不存在');
        }
        //获取分类信息
        $category = model('category')->get($deal->category_id);
        //获取子分类信息
        if ($deal->se_category_id) {
            $se_category = model('category')->get($deal->se_category_id);
        }
        //判断是否开启团购
        $flag = 0;
        if ($deal->start_time > date('Y-m-d H:i:s')) {
            $flag = 1;
            $timedata = '';
            //计算倒计时
            $dtime = strtotime($deal->start_time) - time();
            $d = floor($dtime / (3600 * 24));
            if ($d) {
                $timedata .= $d . '天';
            }
            $i = floor($dtime % (3600 * 24) / 3600);
            if ($i) {
                $timedata .= $i . '小时';
            }
            $s = floor($dtime % (3600 * 24) % 3600 / 60);
            if ($s) {
                $timedata .= $s . '分钟';
            }
            $this->assign(['timedata' => $timedata]);
        }
        //发送经纬度
        $mapstr = $deal->xpoint . ',' . $deal->ypoint;
        //获取分店信息
        $location = model('bisLocation')->getNormalLocationsInId($deal->location_ids);
        return $this->fetch('', ['mapstr' => $mapstr, 'title' => '商品详情', 'location' => $location, 'deal' => $deal, 'category' => $category, 'se_category' => $se_category, 'flag' => $flag]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function priceSum()
    {
        $data = input('post.');
        $price['origin_price'] = $data['num'] * $data['origin_price'];
        $price['current_price'] = $data['num'] * $data['current_price'];
        return json($price);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request $request
     * @param  int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
